package com.ams.protocol.http;

import java.io.File;
import java.io.IOException;

import com.ams.protocol.Context;

public final class HttpContext extends Context {
    private final File contextRoot;

    public HttpContext(String root) {
        contextRoot = new File(root);
    }

    public String getMimeType(String file) {
        int index = file.lastIndexOf('.');
        return (index++ > 0) ? MimeTypes.getContentType(file.substring(index))
                : "unkown/unkown";
    }

    public String getRealPath(String path) {
        return new File(contextRoot, path).getAbsolutePath();
    }

    // security check
    public boolean securize(File file) throws IOException {
        if (file.getCanonicalPath().startsWith(contextRoot.getCanonicalPath())) {
            return true;
        }
        return false;
    }
}
